package org.testmyapp.tasks;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.testmyapp.engine.IEngineServices;
import org.testmyapp.entities.AccountingMedical;
import org.testmyapp.entities.Bill;
import org.testmyapp.entities.InsuredPerson;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;

public class EnterBill implements TaskWorker {

	public String validate(IEngineServices aes,
			Map<String, String[]> parameterMap, String processInstanceId,
			BusinessObjectPersistenceProvider bopp) {

		String careprovider = parameterMap.get("careprovider")[0];
		String billSum = parameterMap.get("billSum")[0];
		String beginDate = parameterMap.get("beginDate")[0];
		String endDate = parameterMap.get("endDate")[0];
		String entryDate = parameterMap.get("entryDate")[0];
		Integer insuredPersonForBill = Integer.parseInt(parameterMap
				.get("insuredPersonForBill")[0]);

		@SuppressWarnings("unchecked")
		List<InsuredPerson> insuredPersons = (List<InsuredPerson>) aes
				.getVariable("insuredPersons", processInstanceId);

		if (insuredPersons == null || insuredPersons.isEmpty()) {
			throw new RuntimeException("No insured persons");
		}

		InsuredPerson ip = null;
		for (InsuredPerson ipIterate : insuredPersons) {
			if (((Integer) ipIterate.getPerson().getPersonId())
					.equals(insuredPersonForBill)) {
				ip = ipIterate;
			}
		}
		
		if(ip == null) {
			throw new RuntimeException("Please choose an ip by person id");
		}

		Bill bill = new Bill();
		bill.setCareProvider(careprovider);
		Date begin = TaskWorkersCommon.parseDateFromString(beginDate);
		bill.setStartDate(begin);
		Date end = TaskWorkersCommon.parseDateFromString(endDate);
		bill.setEndDate(end);
		Date entry = TaskWorkersCommon.parseDateFromString(entryDate);
		bill.setEntryDate(entry);
		bill.setIp(ip);
		bill.setProcessInstanceId(processInstanceId);

		AccountingMedical accountingMedical = (AccountingMedical) aes
				.getVariable("accountingMedical", processInstanceId);
		
		System.out.println("Accounting Medical 4 Bill: " + accountingMedical);
		if(accountingMedical == null) {
			throw new RuntimeException("technical error");
		}
		
		bill.setAccountingMedical(accountingMedical);


		int sum = Integer.parseInt(billSum);
		bill.setBillSum(sum);

		int billnr;
		@SuppressWarnings("unchecked")
		List<Bill> billList = (List<Bill>) aes.getVariable("bills",
				processInstanceId);
		if (billList == null) {
			billList = new ArrayList<Bill>();
		}

		billnr = billList.size() + 1;
		bill.setBillNr(billnr);

		billList.add(bill);
		
		aes.setVariable(processInstanceId, "currentBill", bill);

		aes.setVariable(processInstanceId, "bills", billList);
		
		return "";
	}

	public Date parseDateFromString(String dateString) {
		String[] dateStringArr = dateString.split("\\.");
		Integer[] dateIntArr = new Integer[3];

		try {
			for (int i = 0; i < 3; i++) {
				dateIntArr[i] = Integer.parseInt(dateStringArr[i]);
			}
			@SuppressWarnings("deprecation")
			Date date = new Date(dateIntArr[2] - 1900, dateIntArr[1] - 1,
					dateIntArr[0]);
			return date;
		} catch (Exception e) {
			throw new RuntimeException("Cant parse Date");
		}
	}

}
